﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.DataAnnotations;
using FIFP.DashBoard.Helper;
using FIFP.DashBoard.ViewModel;
using FIFP.DashBoard.Model;
using System.Text.RegularExpressions;

namespace FIFP.DashBoard.Converters
{
    public class ErrorValidation
    {
        public static ValidationResult ValidateScenario(string scenarioName, ValidationContext context)
        {
            if (string.IsNullOrEmpty(scenarioName))
                return new ValidationResult("Must Enter Scenario Name", new[] { context.MemberName });
            else if (!string.IsNullOrEmpty(scenarioName) && ServiceLocator.Get<ImportVM>().IsScenarioExist)
                return new ValidationResult(string.Format(FIFPConstants.SCENARIO_ALREADY_EXISTS, scenarioName), new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateUnitName(string unitName, ValidationContext context)
        {
            //if (string.IsNullOrEmpty(unitName))
            //    return new ValidationResult("Enter UnitName", new[] { context.MemberName });
            if (!string.IsNullOrEmpty(unitName) && (ServiceLocator.Get<ImportVM>().IsUnitNameExist || ServiceLocator.Get<ShutdownMngVM>().IsUnitNameExist))
                return new ValidationResult("UnitName does not exist", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidatePercentage(string percentage, ValidationContext context)
        {
            if(ServiceLocator.Get<ImportVM>().BPercentageValidate)
                return new ValidationResult("The sum of Monitoring and unplanned percentage should not be greater than 100%", new[] { context.MemberName });
            else if (!string.IsNullOrEmpty(percentage) && !Regex.IsMatch(Convert.ToString(percentage), FIFPConstants.RegularExpression))
                return new ValidationResult("Please enter valid number", new[] { context.MemberName });
            else if (!string.IsNullOrEmpty(percentage) && (Convert.ToDouble(percentage) < 0 || Convert.ToDouble(percentage) > 100))
                return new ValidationResult("Enter percentage value between 0 to 100", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateIPR(string iprValue, ValidationContext context)
        {
            if (!string.IsNullOrEmpty(iprValue) && !Regex.IsMatch(Convert.ToString(iprValue), FIFPConstants.RegularExpression))
                return new ValidationResult("Please enter valid number", new[] { context.MemberName });
            else if (!string.IsNullOrEmpty(iprValue) && (Convert.ToDouble(iprValue) < 0 || Convert.ToDouble(iprValue) > 3))
                return new ValidationResult("Enter ipr value between 0 to 3", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateMaxProd(string maxProdValue, ValidationContext context)
        {
            if (!string.IsNullOrEmpty(maxProdValue) && !Regex.IsMatch(Convert.ToString(maxProdValue), FIFPConstants.RegularExpression))
                return new ValidationResult("Please enter valid number", new[] { context.MemberName });
            else if (!string.IsNullOrEmpty(maxProdValue) && Convert.ToDouble(maxProdValue) < 0)
                return new ValidationResult("Please enter +ve Max production value", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateReducedCapacity(string reducedCapacity, ValidationContext context)
        {
            if (!Regex.IsMatch(Convert.ToString(reducedCapacity), FIFPConstants.RegularExpression))
                return new ValidationResult("Please enter valid number", new[] { context.MemberName });
            else if (Convert.ToDouble(reducedCapacity) <= ServiceLocator.Get<ShutdownMngVM>().OriginalCapacity)
                return ValidationResult.Success;
            else
                return new ValidationResult("Reduced Capacity should be lesser than original capacity", new[] { context.MemberName });
        }

        public static ValidationResult ValidateRC(string reducedCapacity, ValidationContext context)
        {
            if (!Regex.IsMatch(Convert.ToString(reducedCapacity), FIFPConstants.RegularExpression))
                return new ValidationResult("Please enter valid number", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateIsNumeric(string newValue, ValidationContext context)
        {
            if (!string.IsNullOrEmpty(newValue) && !Regex.IsMatch(Convert.ToString(newValue), FIFPConstants.RegularExpression))
                return new ValidationResult("Please enter valid number", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateUnitType(string unitType, ValidationContext context)
        {
            if (unitType.Equals(FIFPConstants.SELECT_UNIT_TYPE) && ServiceLocator.Get<ImportVM>().BValidate)
                return new ValidationResult("Must select unit type", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateComboUnitName(Unit unit, ValidationContext context)
        {
            if (unit.UnitName.Equals(FIFPConstants.SELECT_UNITNAME) && ServiceLocator.Get<ImportVM>().BValidate)
                return new ValidationResult("Must select unit name", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateDivision(ActivityType selectedDivision, ValidationContext context)
        {
            if (selectedDivision.Name.Equals(FIFPConstants.SELECT_DIVISION) && ServiceLocator.Get<ImportVM>().BValidate)
                return new ValidationResult("Must select division name", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateActivity(ActivityType selectedActivityCode, ValidationContext context)
        {
            if (selectedActivityCode.Name.Equals(FIFPConstants.SELECT_ACTIVITY) && ServiceLocator.Get<ImportVM>().BValidate)
                return new ValidationResult("Must select activity code", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateLayer(Layer selectedLayer, ValidationContext context)
        {
            if (selectedLayer.LayerName.Equals(FIFPConstants.SELECT_LAYER) && ServiceLocator.Get<ImportVM>().BValidate)
                return new ValidationResult("Must select layer", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateStartDate(DateTime startDate, ValidationContext context)
        {
            if (startDate > ServiceLocator.Get<FilterVM>().EndDate)
                return new ValidationResult("The Start time should be earlier than the End time", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateEndDate(DateTime endDate, ValidationContext context)
        {
            if (endDate < ServiceLocator.Get<FilterVM>().StartDate)
                return new ValidationResult("The Start time should be earlier than the End time", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateActivityStartDate(DateTime startDate, ValidationContext context)
        {
            if (startDate > ServiceLocator.Get<ImportVM>().ActivityEndDate)
                return new ValidationResult("The Start time should be earlier than the End time", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateActivityEndDate(DateTime endDate, ValidationContext context)
        {
            if (endDate < ServiceLocator.Get<ImportVM>().ActivityStartDate)
                return new ValidationResult("The Start time should be earlier than the End time", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateScenarioStartDate(DateTime startDate, ValidationContext context)
        {
            if (startDate > ServiceLocator.Get<ChartViewModel>().EndDate)
                return new ValidationResult("The Start time should be earlier than the End time", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

        public static ValidationResult ValidateScenarioEndDate(DateTime endDate, ValidationContext context)
        {
            if (endDate < ServiceLocator.Get<ChartViewModel>().StartDate)
                return new ValidationResult("The Start time should be earlier than the End time", new[] { context.MemberName });
            else
                return ValidationResult.Success;
        }

    }
}
